﻿using LuShanTea.Common;
using LuShanTea.DTO;
using LuShanTea.Front.Web.App_Start;
using LuShanTea.Front.Web.Models;
using LuShanTea.IService;
using LuShanTea.Web.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LuShanTea.Front.Web.Controllers
{
    public class GoodCommentController : Controller
    {
        public IOrderService OrderService { get; set; }
        public IGoodImsSrcService GoodImgSrcService { get; set; }
        public IOrderRecordService OrderRecordService { get; set; }
        public IIdNameService IdNameService { get; set; }

        public ICommentService CommentService { get; set; }
        // GET: GoodComment
        [IsLogin]
        [HttpGet]
        public ActionResult Comment(long orderRecordId)
        {
            long userId = (long)Session["USERID"];

            var orderRecord = OrderRecordService.GetById(orderRecordId);
            if (orderRecord == null)
            {
                return Content("订单记录不存在");
            }

            var idname = IdNameService.GetIdByTypeNameAndName("订单状态", "已收货");
            if (idname == null)
            {
                throw new Exception("订单状态异常");
            }
            if (!OrderRecordService.CheckUserIsOrderRecord(userId, idname.Id, orderRecordId))
            {
                return Content("收货才能评论");
            }
            CommentListModel listModel = new CommentListModel();
            listModel.OrderRecord = orderRecord;
            listModel.GoodImgSrc = GoodImgSrcService.GetFirstThumImgByGoodId(orderRecord.GoodId);

            return View(listModel);
        }

        [IsLogin]
        [HttpPost]
        public ActionResult Comment(CommentSubModel model)
        {
            //获取用户id
            long userId = (long)Session["USERID"];

            //获取订单记录
            var orderRecord = OrderRecordService.GetById(model.OrderRecordId);

            if (orderRecord == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "订单记录不存在" });
            }

            if (orderRecord.GoodId != model.GoodId)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "商品错误" });
            }

            var idname = IdNameService.GetIdByTypeNameAndName("订单状态", "已收货");
            if (idname == null)
            {
                throw new Exception("订单状态异常");
            }
            if (!OrderRecordService.CheckUserIsOrderRecord(userId, idname.Id, model.OrderRecordId))
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "收货才能评论" });
            }
            CommentDTO dto = new CommentDTO();
            dto.CommentImgSrc = model.CommentImg;
            dto.Content = model.Content;
            dto.GoodId = model.GoodId;
            dto.Level = model.Level;
            dto.OrderId = model.OrderId;
            dto.UserId = userId;
            
            long commentId = CommentService.AddNew(dto);

            orderRecord.CommentId = commentId;
            OrderRecordService.UpdateOrderRecord(orderRecord);

            return Json(new AjaxResult { Status = "ok" });
        }

        [HttpPost]
        [IsLogin]
        public ActionResult Upload(HttpPostedFileBase file)
        {
            if (file.ContentLength <= 0 || file.ContentLength >= 1024 * 1024 * 2)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "文件过大或过小" });
            }

            string md5 = CommonHelper.CalcMD5(file.InputStream);
            string ext = Path.GetExtension(file.FileName);

            string[] exts = { ".jpg", ".png", "gif", ".jpeg", ".bmp" };

            if (!exts.Contains(ext))
            {
                return Json(new AjaxResult
                {
                    Status = "error",
                    ErrorMsg = "请勿上传非图片文件,支持格式( .jpg, .png, gif, .jpeg, .bmp)"
                });
            }

            string path = "upload/userImg/" + DateTime.Now.ToString("yyyy/MM/dd") + "/" + md5 + ext;
            //指针归零
            file.InputStream.Position = 0;
            UploadFile uploadFile = new UploadFile();

            AjaxResult ajaxResult = uploadFile.UploadImg(path, file.InputStream);


            return Json(new AjaxResult { Status = "ok", Data = path });
        }

    }
}